package com.lcq.gulimall.member.advice;

import com.lcq.common.exception.BizCodeEnum;
import com.lcq.common.exception.PhoneExistException;
import com.lcq.common.exception.UserNameExistException;
import com.lcq.common.exception.UserNameORPasswordInvalidException;
import com.lcq.common.utils.R;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice(basePackages = "com.lcq.gulimall.member.controller")
public class MemberControllerAdvice {
    @ExceptionHandler({Exception.class})
    public R exceptionHandler(Exception e){
        if (e instanceof UserNameExistException){
            return R.error(BizCodeEnum.MEMBER_USERNAME_EXIST_EXCEPTION.getCode(),BizCodeEnum.MEMBER_USERNAME_EXIST_EXCEPTION.getMsg());
        } else if (e instanceof PhoneExistException){
            return R.error(BizCodeEnum.MEMBER_PHONE_EXIST_EXCEPTION.getCode(),BizCodeEnum.MEMBER_PHONE_EXIST_EXCEPTION.getMsg());
        } else if (e instanceof UserNameORPasswordInvalidException){
            return R.error(BizCodeEnum.MEMBER_USERNAME_PASSWORD_INVALID.getCode(),BizCodeEnum.MEMBER_USERNAME_PASSWORD_INVALID.getMsg());
        }
        return R.error(BizCodeEnum.UNKNOW_EXEPTION.getCode(),BizCodeEnum.UNKNOW_EXEPTION.getMsg());
    }
}
